package com.peng.online_chat_codec;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * All rights Reserved, Designed by www.rongdasoft.com
 *
 * @author wangshipeng
 * @version: V1.0
 * @Title: ChatFrame.java
 * @Description:
 * @date 2019-11-19 10:27
 * @Copyright: 2019-11-19 www.rongdasoft.com Inc. All rights reseved.
 */
public class ChatFrame extends Frame {
    public static ChatFrame INSTANCE = new ChatFrame();
    public TextArea ta = new TextArea();
    public TextField tf = new TextField();
    private ChatClient chatClient = new ChatClient();

    public ChatFrame() {
        setVisible(true);
        setSize(1280, 720);
        setLocation(300, 200);
        add(ta, BorderLayout.CENTER);
        add(tf, BorderLayout.SOUTH);
        tf.setEditable(false);
        tf.addActionListener(e -> {
            ta.setText(ta.getText() + tf.getText() + "\n");
            chatClient.sendMsg(tf.getText());
            tf.setText("");
        });
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                chatClient.close();
                System.exit(1);
            }
        });
    }

    public static void main(String[] args) {
        ChatFrame.INSTANCE.connect();

    }


    public void connect() {
        chatClient.connectServer();
    }
}
